package com.awspaas.user.apps.processforsap.event;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sofa.common.profile.StringUtil;
import com.awspaas.user.apps.processforsap.util.HttpClientGwUtil;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class GWTaskValidateForFi extends InterruptListener implements InterruptListenerInterface {
    @Override
    public boolean execute(ProcessExecutionContext ctx) throws Exception {
        /**
         * 电子
         */
        boolean isty = ctx.isChoiceActionMenu("同意");


        if (isty) {
            //取值
            ProcessInstance processInstance = ctx.getProcessInstance();
            List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_FIZCLIST").bindId(processInstance.getId()).list();
            BO bo = ctx.getBO("BO_EU_SAP_FIZC");
            String[] type = {"FH00", "FS00"};

            //公司代码

            list = list.stream().filter(bo1 -> "1".equals(bo1.getString("IS_BG"))).collect(Collectors.toList());

            if (list!=null && list.size()>0){
                for (String bukrs:type){
                    String tokenUrl = "http://10.21.89.45:8099/pms/api/session";
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("userno", bukrs.equals("FH00") ? "3312964743-ERP" : "33129661F0-ERP");
                    jsonObject.put("password", bukrs.equals("FH00") ? "zhgw@123" : "zhgw@123");
                    jsonObject.put("isVerify", true);
                    String result = HttpClientGwUtil.doPost(tokenUrl, jsonObject.toJSONString());
                    JSONObject jsonResult = JSONObject.parseObject(result);
                    if (jsonResult.getBoolean("success")) {
                        JSONObject data = jsonResult.getJSONObject("data");
                        String token = data.getString("accessToken");
                        //开始调用业务代码
                        String url = "http://10.21.89.45:8099/gw-edi-up/api/open/v1/erpMat/insertErpMat";
                        JSONObject allData = new JSONObject();
                        JSONArray dataInfo = new JSONArray();
                        for (BO bo1:list){
                            String bgdw = bo1.getString("BGDW");
                            if (StringUtils.isEmpty(bgdw)){
                                throw new BPMNError("0331", "当是否报关选择是时，报关单位不允许为空！");
                            }
                            String value = SDK.getDictAPI().getValue("com.awspaas.user.apps.processforsap","JLDWFORGW",bgdw);

                            JSONObject jsonObject1 = new JSONObject();
                            jsonObject1.put("bondMark", "1");
                            jsonObject1.put("gMark", "2");
                            jsonObject1.put("facGNo", bo1.getString("ANLN1"));
                            jsonObject1.put("copGName",bo1.getString("TXT50"));
                            jsonObject1.put("copGModelEn", bo1.getString("ZXH"));
                            jsonObject1.put("specifiCations", bo1.getString("ZGG"));
                            jsonObject1.put("unitErp",value );
                            jsonObject1.put("createDepartment", "采购处");
                            String sql = "select * from WFH_TASK WHERE (ACTIVITYDEFID = 'obj_cb0c6d42454000013ff743a61a509b90' OR ACTIVITYDEFID='obj_cb0c6d44ef000001f5dc1d60153f9c70') AND PROCESSINSTID = '"+processInstance.getId()+"'";
                            RowMap rowMap = DBSql.getMap(sql);
                            if (rowMap!=null){
                                String target= rowMap.getString("TARGET");
                                jsonObject1.put("makeInsert", SDK.getORGAPI().getUser(target).getUserName()+target);
                            }

//                            String sql = "select * from wfh_task where BINDID = '"+processInstance.getId()+"' and ACTIVITYDEFID='obj_cb0c6d42454000013ff743a61a509b90'";
                            dataInfo.add(jsonObject1);
                        }
                        allData.put("data", dataInfo);
                        String result1 = HttpClientGwUtil.doPost1(url, allData.toJSONString(), token);
                    } else {
                        throw new BPMNError("0331", "意外的接口调用错误！");
                    }
                }
            }
        }
        return true;
    }
}
